local filename = "upvote.lua"

local timetool = require "timetool"
local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog =require "filelog"
require "pubenum"

local service = nil
local CMD = {
	service = nil
}

--
--处理点赞行为
--
function CMD.process(rid, msgbody)
	if not service then
		service = CMD.service
	end

    if not msgbody or not msgbody.upvoted_rid then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "无效的参数"}
    end
	
    --通知被点赞玩家,并更新他的点赞数
    local upvoted_rid = tonumber(msgbody.upvoted_rid)
    if upvoted_rid < 100000 or upvoted_rid > 999999 then
        return { }
    end

    local agent = skynet.call(".mainnode", "lua", "cmd", "get_agent_by_rid", upvoted_rid)
    if agent then
        skynet.send(agent, "lua", "cmd", "inc_upvoted_num",service.info.rolename)
    else  
        -- 去数据节点查询，然后修改
        local upvoted_num = 0
		local result = cluster.call("datanode", ".router", "select_rk", upvoted_rid, "rs_otherinfo",true)
		if result[1] ~= 0 then
			upvoted_num = result[2][upvoted_rid].upvoted_num or 0
            upvoted_num = upvoted_num + 1
            cluster.send("datanode", ".router", "update_rk", upvoted_rid, "rs_otherinfo", nil, {upvoted_num = upvoted_num})
		end
    end

	return {}
end

return CMD